<template>
  <div class="manage">
    <!-- 新增用户对话框 -->
    <el-dialog
      :title="operateType == 'add' ? '新增用户' : '更新用户'"
      :visible.sync="isShow"
    >
      <common-form
        :formLabel="operateFormLabel"
        :form="operateForm"
        :inline="true"
        ref="form"
      ></common-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    <!-- User头部 -->
    <div class="manage-header">
      <!-- User头部--新增按钮 -->
      <el-button type="primary" @click="addUser">+ 新增</el-button>
      <!-- User头部--搜索表单 -->
      <common-form
        :formLabel="formLabel"
        :form="searchForm"
        :inline="true"
        ref="form"
      >
        <el-button type="primary" @click="getList(searchForm.keyword)">搜索</el-button>
      </common-form>
    </div>
    <!-- 用户数据 -->
    <div style="height: 100vh;">
      <common-table
        :tableData="tableData"
        :tableLabel="tableLabel"
        :config="config"
        @changePage="getList()"
        @edit="editUser"
        @del="delUser"
      ></common-table>
    </div>
  </div>
</template>

<script>
import commonForm from '@/components/commomForm.vue'
import commonTable from '@/components/commonTable.vue'
import { getUser } from '@/api/data'
export default {
  name: 'myUser',
  components: {
    commonForm,
    commonTable,
  },
  data() {
    return {
      operateType: 'add',
      isShow: false,
      operateFormLabel: [
        {
          model: 'name',
          label: '姓名',
          type: 'input',
        },
        {
          model: 'age',
          label: '年龄',
          type: 'input',
        },
        {
          model: 'sex',
          label: '性别',
          type: 'select',
          opts: [
            {
              label: '男',
              value: 1,
            },
            {
              label: '女',
              value: 0,
            },
          ],
        },
        {
          model: 'order',
          label: '订单号',
          type: 'input',
        },
        {
          model: 'addr',
          label: '地址',
          type: 'input',
        },
      ],
      operateForm: {
        name: '',
        addr: '',
        age: '',
        birth: '',
        sex: '',
      },
      formLabel: [
        {
          model: 'keyword',
          label: '',
          type: 'input',
        },
      ],
      searchForm: {
        keyword: '',
      },
      tableData: [],
      tableLabel: [
        {
          prop: 'name',
          label: '姓名',
        },
        {
          prop: 'age',
          label: '年龄',
        },
        {
          prop: 'sexLabel',
          label: '性别',
        },
        {
          prop: 'order',
          label: '订单号',
          width: 200,
        },
        {
          prop: 'addr',
          label: '地址',
          width: 320,
        },
      ],
      config: {
        page: 1,
        total: 30
      },
    }
  },
  methods: {
    confirm() {
      if (this.operateForm === 'edit') {
        this.$http.post('/user/edit', this.operateForm).then((res) => {
          console.log(res)
          this.isShow = false
          this.getList()
        })
      } else {
        this.$http.post('/user/add', this.operateForm).then((res) => {
          console.log(res)
          this.isShow = false
          this.getList()
        })
      }
    },
    addUser() {
      this.isShow = true
      this.operateType = 'add'
      this.operateForm = {
        name: '',
        addr: '',
        age: '',
        order: '',
        sex: '',
      }
    },
    getList(name = '') {
      this.config.loading = true
      name ? (this.config.page = 1) : ''
      getUser({
        page: this.config.page,
        name
      }).then(({ data: res }) => {
        this.tableData = res.list.map(item => {
         item.sexLabel = item.sex === 0 ? '女' : '男'
          return item
        })
        
        console.log(res,'res')
        this.config.total = res.count
        this.config.loading = false
      })
      
    },
    editUser(row) {
      this.operateType = 'edit'
      this.isShow = true
      this.operateForm = row
    },
    delUser(row) {
      this.$confirm('此操作将永久删除该文件，是否继续','提示',{
        confirmButtonText:'确定',
        cancelButtonText:'取消',
        type:'warning'
      }).then(()=>{
        const id = row.id
        this.$http.post('/user/del',{
          params:{id}
        }).then(()=>{
          this.$message({
            type:'success',
            message:'删除成功'
          })
          this.getList()
        })
      })
    },
  },
  created() {
    this.getList()
  },
}
</script>

<style lang="less" scoped>
.manage-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
